package pratice.fengzhongye.interview;

import org.junit.Test;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 面试题：请讲一下，hashSet, hashMap, equals, hashcode之前的关系
 *
 * @author 吴宇亮 on 2019/2/19 0019 11:36
 */
public class Lesson6_HashSet {

    @Test
    public void test(){
        Set<Person> set = new HashSet();
        set.add(new Person("zhangsan"));
        set.add(new Person("wangwu"));
        set.add(new Person("zhangsan"));

        for (Person person : set) {
            System.out.println(person.name);
        }
    }

}

class Person{

    String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
